package com.jiazhong.IO流.字节流;

import java.io.*;

public class FileCopy {
    public static void main(String[] args) throws IOException {
        File src=new File("D:\\Study\\Navicat.rar");
        File desc=new File("D:\\test\\Navicat.rar");
        long star=System.currentTimeMillis();
        fileCopy1(src,desc);
        long end=System.currentTimeMillis();
        System.out.println("耗时"+(end-star));
    }
    /**
     * 文件复制
     * @param src   源文件
     * @param desc  目标文件
     */
    private static void fileCopy(File src, File desc) throws IOException {
        //创建字节输入流对象和字节输出流对象
        FileInputStream inputStream = new FileInputStream(src);
        FileOutputStream outputStream = new FileOutputStream(desc);
        //设置缓冲区，指定一次读取的数据量
        byte[] buf = new byte[1024*1024*5];
        int len=-1;
        while ((len=inputStream.read(buf))!=-1){
            //将读取的数据写出到磁盘
            outputStream.write(buf,0,len);
            //刷新输出流
            outputStream.flush();
        }
        outputStream.close();
    }

    private static void fileCopy1(File src, File desc) throws IOException {
        //创建字节输入流对象和字节输出流对象
        FileInputStream inputStream = new FileInputStream(src);
        FileOutputStream outputStream = new FileOutputStream(desc);

        BufferedInputStream bufferedInputStream=new BufferedInputStream(inputStream);
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);
        //设置缓冲区，指定一次读取的数据量
        byte[] buf = new byte[1024*1024*5];
        int len=-1;
        while ((len=bufferedInputStream.read(buf))!=-1){
            //将读取的数据写出到磁盘
            bufferedOutputStream.write(buf,0,len);
            //刷新输出流
            bufferedOutputStream.flush();
        }
        bufferedOutputStream.close();
    }
}
